#Charles S. Bullock, III, William D. Hicks, 
#M.V. Hood, III, Seth C. McKee, and Daniel A. Smith
#"The Election of African American State Legislators in the Modern South" in LSQ

#THIS FILE REPRODUCES FIGURE A1 IN ONLINE APPENDIX

rm(list=ls())


#SET A WORKING DIRECTORY TO SAVE FIGURE
setwd("[MAKE DIRECATORY HERE]")

#LOAD DATASET FIGURE_1_A1.Rdata
load("~[FILE PATH]/FIGURE_1_A1.Rdata")


dt2$perblack <- round(dt2$black/dt2$seats, 2)
dt2$pergop <- round(dt2$gop/dt2$seats, 2)

png('FIGURE_A1.png', width=900, height=700, pointsize=16)
par(mfrow=c(4,3), mar=c(3,3,2,2))

#Alabama

alsp <- data.frame(c(1970:2014))
alsp2 <- dt2[dt2$v02=='AL', c('v05','pergop','perblack')]
alsp2 <- merge(alsp2,alsp,by.x='v05',by.y='c.1970.2014.',all=TRUE)
miss <- !is.na(alsp2$pergop)

plot(which(miss), alsp2$perblack[miss], main="AL", cex.main=1.5, type="o", pch=16, ylim=c(0,.75), cex=2, lwd=2, lty=1, cex.lab=6, axes=FALSE, xlab="", ylab="")
lines(which(miss), alsp2$pergop[miss], type="o", pch=15, lty=1, lwd=2, cex=2)
axis(2, pos= c(.5,75), at=c(0,.25,.5,.75), labels=c('0%','25%','50%','75%'))
axis(1, at=c(1,9,17,25,33,41,45), lab=c('70','78','86','94','02','10','14'), cex=1)
axis(1, at=45, lab='14')


#Arkansas

plot(dt2$perblack[dt2$v02=='AR'], main='AR', type='o', cex.main=1.5, pch=16, ylim=c(0,.75), cex=2, lwd=2, lty=1, cex.lab=6, axes=FALSE, xlab="", ylab="")
lines(dt2$pergop[dt2$v02=='AR'], type="o", pch=15, lty=1, lwd=2, cex=2)
axis(1, at=c(1,5,9,13,17,21,24), labels=c('68','76','84','92','00','08','14'), cex=1)
axis(2, pos= c(.5,75), at=c(0,.25,.5,.75), labels=c('0%','25%','50%','75%'))

#Florida

plot(dt2$perblack[dt2$v02=='FL'], main='FL', type='o', cex.main=1.5, pch=16, ylim=c(0,.75), cex=2, lwd=2, lty=1, cex.lab=6, axes=FALSE, xlab="", ylab="")
lines(dt2$pergop[dt2$v02=='FL'], type="o", pch=15, lty=1, lwd=2, cex=2)
axis(1, at=c(1,5,9,13,17,21,24), labels=c('68','76','84','92','00','08','14'), cex=1)
axis(2, pos= c(.5,75), at=c(0,.25,.5,.75), labels=c('0%','25%','50%','75%'))

#Georgia

plot(dt2$perblack[dt2$v02=='GA'], main='GA', type='o', cex.main=1.5, pch=16, ylim=c(0,.75), cex=2, lwd=2, lty=1, cex.lab=6, axes=FALSE, xlab="", ylab="")
lines(dt2$pergop[dt2$v02=='GA'], type="o", pch=15, lty=1, lwd=2, cex=2)
axis(1, at=c(1,5,9,13,17,21,24), labels=c('68','76','84','92','00','08','14'), cex=1)
axis(2, pos= c(.5,75), at=c(0,.25,.5,.75), labels=c('0%','25%','50%','75%'))

#Louisiana


lasp <- data.frame(c(1968:2015))
lasp2 <- dt2[dt2$v02=='LA', c('v05','pergop','perblack')]
lasp2 <- merge(lasp2,lasp,by.x='v05',by.y='c.1968.2015.',all=TRUE)
miss2 <- !is.na(lasp2$pergop)

plot(which(miss2), lasp2$perblack[miss2], main="LA", cex.main=1.5, type="o", pch=16, ylim=c(0,.75), cex=2, lwd=2, lty=1, cex.lab=6, axes=FALSE, xlab="", ylab="")
lines(which(miss2), lasp2$pergop[miss2], type="o", pch=15, lty=1, lwd=2, cex=2)
axis(2, pos= c(.5,75), at=c(0,.25,.5,.75), labels=c('0%','25%','50%','75%'))
axis(1, at=c(1,8,16,24,32,40,48), lab=c('68','75','83','91','99','07','15'), cex.axis=1)


#Mississippi

mssp <- data.frame(c(1971:2015))
mssp2 <- dt2[dt2$v02=='MS', c('v05','pergop','perblack')]
mssp2 <- merge(mssp2,mssp,by.x='v05',by.y='c.1971.2015.',all=TRUE)
miss3 <- !is.na(mssp2$pergop)

plot(which(miss3), mssp2$perblack[miss3], main="MS", cex.main=1.5, type="o", pch=16, ylim=c(0,.75), cex=2, lwd=2, lty=1, cex.lab=6, axes=FALSE, xlab="", ylab="")
lines(which(miss3), mssp2$pergop[miss3], type="o", pch=15, lty=1, lwd=2, cex=2)
axis(2, pos= c(.5,75), at=c(0,.25,.5,.75), labels=c('0%','25%','50%','75%'))
axis(1, at=c(1,9,17,25,33,45), lab=c('71','79','87','95','03','15'), cex.axis=1)
axis(1, at=41, lab='11', cex.axis=1)

#North Carolina

plot(dt2$perblack[dt2$v02=='NC'], main='NC', type='o', cex.main=1.5, pch=16, ylim=c(0,.75), cex=2, lwd=2, lty=1, cex.lab=6, axes=FALSE, xlab="", ylab="")
lines(dt2$pergop[dt2$v02=='NC'], type="o", pch=15, lty=1, lwd=2, cex=2)
axis(1, at=c(1,5,9,13,17,21,23), labels=c('70','78','86','94','02','10','14'), cex=1)
axis(1, at=23, lab='14')
axis(2, pos= c(.5,75), at=c(0,.25,.5,.75), labels=c('0%','25%','50%','75%'))

#South Carolina

plot(dt2$perblack[dt2$v02=='SC'], main='SC', type='o', cex.main=1.5, pch=16, ylim=c(0,.75), cex=2, lwd=2, lty=1, cex.lab=6, axes=FALSE, xlab="", ylab="")
lines(dt2$pergop[dt2$v02=='SC'], type="o", pch=15, lty=1, lwd=2, cex=2)
axis(1, at=c(1,5,9,13,17,21,24), labels=c('68','76','84','92','00','08','14'), cex=1)
axis(2, pos= c(.5,75), at=c(0,.25,.5,.75), labels=c('0%','25%','50%','75%'))

##Tennessee

plot(dt2$perblack[dt2$v02=='TN'], main='TN', type='o', cex.main=1.5, pch=16, ylim=c(0,.75), cex=2, lwd=2, lty=1, cex.lab=6, axes=FALSE, xlab="", ylab="")
lines(dt2$pergop[dt2$v02=='TN'], type="o", pch=15, lty=1, lwd=2, cex=2)
axis(1, at=c(1,5,9,13,17,21,24), labels=c('68','76','84','92','00','08','14'), cex=1)
axis(2, pos= c(.5,75), at=c(0,.25,.5,.75), labels=c('0%','25%','50%','75%'))


##Texas

plot(dt2$perblack[dt2$v02=='TX'], main='TX', type='o', cex.main=1.5, pch=16, ylim=c(0,.75), cex=2, lwd=2, lty=1, cex.lab=6, axes=FALSE, xlab="", ylab="")
lines(dt2$pergop[dt2$v02=='TX'], type="o", pch=15, lty=1, lwd=2, cex=2)
axis(1, at=c(1,5,9,13,17,21,24), labels=c('68','76','84','92','00','08','14'), cex=1)
axis(2, pos= c(.5,75), at=c(0,.25,.5,.75), labels=c('0%','25%','50%','75%'))

#Virginia

vasp <- data.frame(c(1969:2015))
vasp2 <- dt2[dt2$v02=='VA', c('v05','pergop','perblack')]
vasp2 <- merge(vasp2,vasp,by.x='v05',by.y='c.1969.2015.',all=TRUE)
miss4 <- !is.na(vasp2$pergop)

plot(which(miss4), vasp2$perblack[miss4], main="VA", cex.main=1.5, type="o", pch=16, ylim=c(0,.75), cex=2, lwd=2, lty=1, cex.lab=6, axes=FALSE, xlab="", ylab="")
lines(which(miss4), vasp2$pergop[miss4], type="o", pch=15, lty=1, lwd=2, cex=2)
axis(2, pos= c(.5,75), at=c(0,.25,.5,.75), labels=c('0%','25%','50%','75%'))
axis(1, at=c(1,9,17,25,33,41,47), lab=c('69','77','85','93','01','09','15'), cex.axis=1)


plot(1, axes=FALSE, type = "n", xlab='', ylab='',,bty = "n", xaxt = "n", yaxt = "n")

legend('top', inset=c(0,-.05), c('% GOP','% Black'), cex=2.5, 
       pt.cex=3.5, col=c('black','black'), pch=c(15,16), bty='n')

dev.off()